#!/usr/bin/perl

use Data::Dumper;

do "../web-lib.pl";

#&init_config();

#%hwinfo=&hwinfo("diskman");

@diskarr=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

#----get disk capacity infomation----------#
sub getdiskindex
{   
    for($j=0; $j<16; $j++){
        $darr[$j]->{'id'} = $diskarr[$j];
        $darr[$j]->{'capacity'} = null;
    }
    $out=`megarc.bin -logphysinfo -a0 > /root/ut/diskindex`;
    open(GDI, "/root/ut/diskindex");
    @dindex = <GDI>;
    foreach $inf (@dindex) {
        $inf =~ s/\r|\n//g;
        $inf =~ s/\*|\t//g;
        if($inf =~ /(\d+)MB\sdrive\sscsi\sID\s(\d+)/){
           $diskid = $2;
           $diskcap = $1;
	   $darr[$diskid]->{'id'} = $diskid;
           $darr[$diskid]->{'capacity'} = $diskcap;   
        }
        else{
           next;
        }
    }
    close(GDI);
    return @darr;
}

#------Get disk style infomation: vendor and product style----#
sub getdiskstyle
{
    for($j=0; $j<16; $j++){
        $stylearr[$j]->{'id'} = $diskarr[$j];
        $stylearr[$j]->{'style'} = "null";
    }    
    $out=`megarc.bin -phys -a0 -chall -idall> /root/ut/diskstyle`;
    open(GDS, "/root/ut/diskstyle");
    @dstyle = <GDS>;
    $mark = 0;
    foreach $ds (@dstyle) {
        $ds =~ s/\r|\n//g;
        $ds =~ s/\*|\t//g;
        if ($ds =~ /Adapter\s\d+,\sChannel\s\d,\sTarget\sID\s(\d+)-+/){
            $diskid = $1; 
            $mark=1;
            $stylearr[$diskid]->{'id'} = $diskid;
            next;
        }
        elsif ($ds =~ /Vendor\s+:\s(\S+)/) {
            $vendor = $1;
            $stylearr[$diskid]->{'style'} = $vendor." ";
            $mark=1;
            next;
        }
        elsif ($ds =~ /Product:\s(\S+)\s+/) {
            $disksty = $1; $stylearr[$diskid]->{'style'} .= $disksty;$mark=0;
            next;		
        }
        else{
            $mark = 0;
            next;
        }
    }
    close(GDS);
    return @stylearr;
}

#-------Get Disk Status---such as included in which logical drive, status---#
sub getdiskstatus
{
    for($j=0; $j<16; $j++){
        $statusarr[$j]->{'id'} = $diskarr[$j];
        $statusarr[$j]->{'status'} = "null";
        $statusarr[$j]->{'unit'} = "null";
    }
    $out=`megarc.bin -ldinfo -a0 -lall > /root/ut/diskstatus`;
    open(STAT, "/root/ut/diskstatus");
    @dstatus = <STAT>;
    $ldline = @dstatus;
    close(STAT);
    for($i=0; $i<$ldline; $i++) {
        $dstatus[$i] =~ s/\r|\n//g;
        $dstatus[$i] =~ s/\*|\t//g;
        if($dstatus[$i] =~ /Logical\sDrive\s(\d)\s:/){ 
           $unit = $1;
           $n = $i + 3;
           $mark = 1;
           while($mark) {
                 if($dstatus[$n] =~ /\d\s+(\d+)\s+\S+\s+\s+\S+\s+(\S+)/){
                    $disk = int($1); #print $disk;
                    $status = $2;
                    $statusarr[$disk]->{'unit'} = $unit;
                    $statusarr[$disk]->{'status'} = $status;
		    $n++;
		    $mark=1;
                 }
                 else{
                    $mark = 0;
                 }
           }
        }
        else{
           next;
        }
    }
    return @statusarr;

}

#-----Get Logical Drive Infomation-----------#
sub get_mega_raid_info
{
    $out=`megarc.bin -ldinfo -a0 -lall > /root/ut/lsildinfo`;
    open(UNIT, "/root/ut/lsildinfo");
    @unitfile = <UNIT>;
    close(UNIT);
    $i = 0;
    foreach $unit (@unitfile) {
        $unit =~ s/\r|\n//g;
        $unit =~ s/\*|\t//g;
    }
    for($i=0; $i<@unitfile; $i++) { 
        @udisks = ();
        if($unitfile[$i] =~/\s+Logical\sDrive\s:\s(\d).*Status:\s(\S+)/) {
           $unitid = $1;
           $status = $2;
           $unitarr[$unitid]->{'id'} = $unitid;
           $unitarr[$unitid]->{'status'} = $status;
           $mark = 1;
           
	   for($j=$i+1; ($mark==1)&&($j<@unitfile); $j++){
               if ($unitfile[$j] =~ /RaidLevel:\s(\S+)\s+/){
                   $level = $1;
                   $unitarr[$unitid]->{'level'} = $level;
                   $mark=1;
               } 
               elsif ($unitfile[$j] =~ /0\s+(\d+)\s+\S+\s+\S+\s+(\S+)/) {
                   $devs =  int($1).$2;
                   $unitarr[$unitid]->{'devs'} .= $devs;
                   push(@udisks, $devs);
                   $mark=1;
               }
               elsif($unitfile[$j] =~ /\s+Logical\sDrive\s:\s\d.*Status/){
                     $mark=0;
               }
           }
#          $unitarr[$unitid]->{'devs'} = @udisks;
        }
        else{
           next;
        }
    }
    return @unitarr;
}
#------------------------------------------#
sub get_all_lsi_diskinfo
{
    @freedisks =();
    @dsi = getdiskindex();
    @dsa = getdiskstyle();
    @dstat = getdiskstatus();
  for($i=0;$i<16;$i++) {
    $lsidinfo[$i]->{'id'} = $dsi[$i]->{'id'};
    $lsidinfo[$i]->{'capacity'} = $dsi[$i]->{'capacity'};
    $lsidinfo[$i]->{'style'} = $dsa[$i]->{'style'};
    $lsidinfo[$i]->{'unit'} = $dstat[$i]->{'unit'};
    $lsidinfo[$i]->{'status'} = $dstat[$i]->{'status'};
    if(($lsidinfo[$i]->{'capacity'} ne 'null')&&($lsidinfo[$i]->{'unit'} eq 'null')) {
         push(@freedisks, $lsidinfo[$i]->{'id'}); print $dsi[$i]->{'id'};
    }
  }
return @lsidinfo;
}

sub get_free_lsi_unit
{  
   @uindex=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
   
   @uarr = get_mega_raid_info();
   for($i=0;$i<@uindex;$i++){
       $mark = 0;
       foreach $u (@uarr) {
           if($u->{'id'} eq $uindex[$i]) {
              $mark = 1;
           }
           if($mark){
              last;
           }
           else{
              next;
           }
       }
       if($mark){
          next;
       }
       else{
          return $uindex[$i];
       }
   }
}

$uu=get_free_lsi_unit();
print $uu."\n";
